<?php

namespace Frieza\Modules\Position\Controllers;

use Doraemon\model\Position\PositionExtend;
use Doraemon\pockets\constInterface\PopupServiceConf;
use Doraemon\service\Popups\PopupsApi;
use Encore\Admin\Grid;
use Encore\Admin\Widgets\Form;
use Illuminate\Http\Request;


class SearchResultPopupController extends BaseController
{
    use BaseTrait;

    public function extendDefaultBaseInfo()
    {
        return [
            'popups' => [
                'type' => PopupServiceConf::STYLE_LOTTIE
            ],
            'key_word' => $this->relation['ad_unique'] ?? ''
        ];
    }

    public function beforeTitleForm(Form $form)
    {
        $form->text('key_word', '关键词')->required()->help("多个关键词使用,隔开;例如:厨房,客厅");
        $form->radio('base.ext_type', '广告')->options([0 => '否', 1 => '是']);
        $form->customPopups('popups', '弹窗配置', $form, $this->imgHelp(), ['append' => PopupServiceConf::STYLE_LOTTIE, 'remove' => [PopupServiceConf::STYLE_CUSTOM,PopupServiceConf::STYLE_CHECK_LINK]]);
    }

    public function saveModule(Request $request, $baseInfoId)
    {
        $popupsData = $request->input('popups', []);
        $key_word = $request->input('key_word', '');

        if ($popupsData['type'] == PopupServiceConf::STYLE_FLOATING) {
            $pic = $popupsData['bgPic'] ?? '';
            if (empty($pic)) throw new \Exception('图片必须上传');
        }
        PopupsApi::savePopups($popupsData, $baseInfoId);
        PositionExtend::saveSearchResultPopup($baseInfoId, $key_word);
    }

    public function extendList(Grid $grid)
    {
        $grid->column('ext->key_word', '关键词');
    }
}
